package com.his.organization.controller;

import com.his.common.verification.group.Update;
import com.his.config.annotation.CurrentUser;
import com.his.login.entity.DictEmployee;
import com.his.organization.service.OrganizationService;
import com.his.organization.vo.OrganizationVo;
import com.xiao.common.controller.ResultBaseController;
import com.xiao.common.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/6 15:24
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/api/org")
public class OrganizationgController extends ResultBaseController {
    private  final Logger logger = (Logger) LoggerFactory.getLogger(this.getClass());
    @Resource
    private OrganizationService organizationService;


    @PostMapping(value = "/save.do")
    public ResultUtil save(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated OrganizationVo organizationVo){
        try{
            organizationService.save(dictEmployee,organizationVo);
            return this.success("保存成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }

    @PostMapping(value = "/update.do")
    public ResultUtil update(@CurrentUser DictEmployee dictEmployee,@RequestBody @Validated({Update.class}) OrganizationVo organizationVo){
        try{
            organizationService.update(organizationVo);
            return this.success("保存成功！",null);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }

    @GetMapping(value = "/get_by_id.do")
    public ResultUtil getOrgById(@CurrentUser DictEmployee dictEmployee,String orgId){
        try{
            return this.success("",organizationService.selectById(orgId));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }

    }
    @PostMapping(value="/get_by_condition.do")
    public ResultUtil getOrgByUpid(@CurrentUser DictEmployee dictEmployee, @RequestBody OrganizationVo organizationVo){
        try{
            return this.success("",organizationService.selectByEntity(dictEmployee,organizationVo));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }
    @GetMapping(value="/get_org_tree.do")
    public ResultUtil getOrgTree(@CurrentUser DictEmployee dictEmployee){
        try{
            return this.success("",organizationService.getOrgTree(dictEmployee));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return this.fail(e.getMessage(),null);
        }
    }

}
